package com.yuntu.service;

import com.yuntu.dao.UserMapper;
import com.yuntu.pojo.User;
import com.yuntu.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String devCode, String devPassword) throws Exception {
        User user=userMapper.getUserLogin(devCode);
        if (user!=null){
            if (!user.getDevPassword().equals(devPassword)){
                user=null;
            }
        }
        return user;
    }

    @Override
    public List<User> getPageAll(PageUtil<User> pageUtil) throws Exception {
        List<User> list=new ArrayList<>();
        int count=userMapper.getCount();
        System.out.println("---"+count);
        pageUtil.setCounts(count);
        if (count>0){
            if (pageUtil.getPageindex()>pageUtil.getPagecount()){
                pageUtil.setPageindex(pageUtil.getPagecount());
            }
            list=userMapper.getPageAll(pageUtil);
            pageUtil.setLists(list);
        }
        return list;
    }

    @Override
    public User getAllById(int id) throws Exception {
        User user=userMapper.getAllById(id);
        return user;
    }

    @Override
    public Boolean upd(User user) throws Exception {
        Boolean flag=false;
        if (userMapper.upd(user)>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean del(int id) throws Exception {
        Boolean flag=false;
        if (userMapper.del(id)){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean add(User user) throws Exception {
        Boolean flag=false;
        if (userMapper.add(user)){
            flag=true;
        }
        return flag;
    }
}
